<?php
class Framework{
    public static function run(){
        Framework::initConst();
        Framework::initConfig();
        Framework::initRoutes();
        Framework::initAutoLoad();
        Framework::initDispatch();
    }
    // 定义路径常量
    private static function initConst(){
        define('DS',DIRECTORY_SEPARATOR);
        define('ROOT_PATH',getcwd().DS);
        define('APP_PATH',ROOT_PATH.'Application'.DS);
        define('CONFIG_PATH',APP_PATH.'Config'.DS);
        define('CONTROLLER_PATH',APP_PATH.'Controller'.DS);
        define('MODEL_PATH',APP_PATH.'Model'.DS);
        define('VIEW_PATH',APP_PATH.'View'.DS);
        define('FRAMEWORK_PATH',ROOT_PATH.'Framework'.DS);
        define('CORE_PATH',FRAMEWORK_PATH.'Core'.DS);
        define('LIB_PATH',FRAMEWORK_PATH.'Lib'.DS);
        define('TRAITS_PATH',ROOT_PATH.'Traits'.DS);
    }
    private static function initConfig(){
        $GLOBALS['config']=require CONFIG_PATH.'config.php';
        // echo  CONFIG_PATH.'config.php';
        // exit;
        // echo $GLOBALS['config'];
    }
    //确定路由
    private static function initRoutes(){
        // echo $GLOBALS['config'];
        $p=$_GET['p']??$GLOBALS['config']['app']['dp'];
        $c=$_GET['c']??$GLOBALS['config']['app']['dc'];
        $a=$_GET['a']??$GLOBALS['config']['app']['da'];
        $p=ucfirst(strtolower($p));
        $c=ucfirst(strtolower($c));   //转成首字母大写
        $a=strtolower($a);            //全转成小写
        define('PLATFROM_NAME',$p);     //平台名常量
        define('CONTROLLER_NAME',$c);   //控制器名常量
        define('ACTION_NAME',$a);       //方法名名常量
        define('__URL__',CONTROLLER_PATH.$p.DS);  //当前请求控制器的地址
        define('__VIEW__',VIEW_PATH.$p.DS);
    }
    private static function initAutoLoad(){
        spl_autoload_register(function($class_name){
            $namespace=dirname($class_name); //命名空间
            $class_name=basename($class_name); //累名
            if(in_array($namespace,array('Core','Lib'))){ //命名空间在core和lib下
                $path=FRAMEWORK_PATH.$namespace.DS.$class_name.'php';
            }elseif($namespace=='Model'){ //文件在model下
                $path=MODEL_PATH.$class_name.'php';
            }elseif($namespace=='Traits'){ //文件在traits下
                $path=TRAITS_PATH.$class_name.'php';
            }else{
                $path=CONTROLLER_PATH.PLATFROM_NAME.DS.$class_name.'.php';
            }
            // require "./{$class_name}.php";
            if(file_exists($path) && is_file($path)){
                require $path;
            }
        });
    }
    private static function initDispatch(){
        $controller_name='\Controller\\'.PLATFROM_NAME.'\\'.CONTROLLER_NAME.'Controller';//拼接控制器类名
        $action_name=ACTION_NAME.'Action';   //拼接方法名
        //请求方法
        $obj=new $controller_name();
        $obj->$action_name();
    
    }
}

?>